//
//  SFBaseViewController.swift
//  SFWeiboPro
//
//  Created by happy on 2017/9/6.
//  Copyright © 2017年 happy. All rights reserved.
//

import UIKit

class SFBaseViewController: UIViewController {
    
    ///访客用户信息
    var visitorInfoDict: [String: String]?
    
    var tableView: UITableView?
    ///刷新控件
    var refreshControl: UIRefreshControl?
    ///判断是否实现上拉加载更多
    var isPullUp = false
    

    lazy var navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.yw_screenWidth(), height: 64))
    
    lazy var navItem = UINavigationItem()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
        loadData()
        
        NotificationCenter.default.addObserver(self, selector: #selector(successLongin), name: NSNotification.Name(rawValue: SFUserLogonSuccessNotification), object: nil)
    }
    
    //数据源加载数据
    func loadData() {
        refreshControl?.endRefreshing()
    }

    //重写title 的set 方法
    override var title: String? {
        didSet {
            navItem.title = title
        }
    }
    
    deinit {
        
        NotificationCenter.default.removeObserver(self)
        
    }
}

// MARK: UI界面设置
extension SFBaseViewController {
    
    func setupUI() {
        view.backgroundColor = UIColor.white
        
        //取消自动缩进, 如果隐藏了导航栏,会缩进20个点
        automaticallyAdjustsScrollViewInsets = false
        
        setupNavgationBar()
        
        SFNetworkManager.shareManager.userLogon ? setupTabelView() : setupVistorView()
    }
    
    //添加tabelView
    func setupTabelView() {
        tableView = UITableView(frame: view.bounds, style: .plain)
        
        view.insertSubview(tableView!, belowSubview: navigationBar)
        
        //设置代理
        tableView?.delegate = self
        tableView?.dataSource = self
        
        //设置内容缩进
        tableView?.contentInset = UIEdgeInsets(top: navigationBar.bounds.height, left: 0,
                                               bottom: tabBarController?.tabBar.bounds.height ?? 49, right: 0)
        
        tableView?.scrollIndicatorInsets = tableView!.contentInset
        //添加刷新控件
        refreshControl = UIRefreshControl()
        tableView?.addSubview(refreshControl!)
        
        refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
    }
    
    //添加访客视图/Users/happy/SK/swift/swiftPro/SFWeibo/SFWeiboPro/Podfile
    func setupVistorView() {
        let vistorView = SFVisitorView(frame: view.bounds)
        
        view.insertSubview(vistorView, belowSubview: navigationBar)
        
        vistorView.visitorInfo = visitorInfoDict
        
        vistorView.registerButton.addTarget(self, action: #selector(registerBtn), for: .touchUpInside)
        vistorView.loginButton.addTarget(self, action: #selector(loginBtn), for: .touchUpInside)
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(registerBtn))
        navItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(loginBtn))
    }
    
    @objc func successLongin(n: Notification) {
        
        navItem.leftBarButtonItem = nil
        navItem.rightBarButtonItem = nil
        
        view = nil
        
        NotificationCenter.default.removeObserver(self)
    }

    
    @objc private func registerBtn() {
        
        print("点击注册按钮")
    }
    
    @objc private func loginBtn() {
        
        print("点击登录按钮")
        
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: SFUserLoginNotification), object: nil)
    }
    
    //添加导航栏
    private func setupNavgationBar() {
        //添加导航条
        view.addSubview(navigationBar)
        
        navigationBar.items = [navItem]
        //设置导航栏的背景颜色
        navigationBar.barTintColor = UIColor.yw_color(withHex: 0xf6f6f6)
        //导航条文字颜色
        navigationBar.tintColor = UIColor.orange
        //标题颜色
        navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.darkGray];
    }
    
}

// MARK: UITableViewDataSource, UITableViewDelegate
extension SFBaseViewController: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 10
    }
    //实现上拉刷新
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        let row = indexPath.row
        let section = tableView.numberOfSections - 1
        
        if row < 0 && section < 0 {
            return
        }
        
        let count = tableView.numberOfRows(inSection: section)
        
        if row == (count - 1) && !isPullUp {
            print("上拉刷新")
//            loadData()
            
            isPullUp = true
        }
        
        
    }
    
}




